/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author tomeu
 */
public class CommonLib {

    public Date stringToDate(String strdate, String format) {
        Date date = null;
        try {
            SimpleDateFormat formatDate = new SimpleDateFormat(format);
            date = formatDate.parse(strdate);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return date;
    }

    public String dateToString(Date date, String format) {

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);
        return sdf.format(date);
    }

    public long calculateDays(Date dateEarly, Date dateLater) {
        return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);

    }

    public Date addDay(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, days);
        return cal.getTime();
    }
}
